package com.pet.serve.service;

import com.pet.serve.entity.Collect;
import com.pet.serve.entity.Collect.collectType;
import com.pet.serve.repository.CollectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CollectService {

    private final CollectRepository collectRepository;

    @Autowired
    public CollectService(CollectRepository collectRepository) {
        this.collectRepository = collectRepository;
    }

    /**
     * 获取指定用户名的所有收藏
     */
    public List<Collect> getCollectsByUsername(String username) {
        return collectRepository.findByUsername(username);
    }

    /**
     * 获取指定用户名和收藏类型的收藏记录
     */
    public List<Collect> getCollectsByUsernameAndType(String username, collectType type) {
        return collectRepository.findByUsernameAndType(username, type);
    }

    /**
     * 根据用户名和商品编号删除收藏
     */
    @Transactional
    public void deleteCollectByGoodId(String username, Long goodid) {
        collectRepository.deleteByUsernameAndGoodid(username, goodid);
    }

    /**
     * 根据用户名和宠物编号删除收藏
     */
    @Transactional
    public void deleteCollectByPetId(String username, Long petid) {
        collectRepository.deleteByUsernameAndPetid(username, petid);
    }

    /**
     * 获取指定用户名的所有有效收藏（状态为 active）
     */
    public List<Collect> getActiveCollectsByUsername(String username) {
        return collectRepository.findActiveCollectsByUsername(username);
    }

    /**
     * 获取指定用户名、收藏类型和状态的收藏记录
     */
    public List<Collect> getCollectsByUsernameTypeAndStatus(String username, collectType type, String status) {
        return collectRepository.findByUsernameAndTypeAndStatus(username, type, status);
    }

    /**
     * 判断商品是否在收藏列表中
     */
    public boolean isGoodCollected(String username, Long goodid) {
        List<Collect> collects = collectRepository.findByUsernameAndType(username, collectType.good);
        return collects.stream().anyMatch(collect -> goodid.equals(collect.getGoodid()));
    }

    /**
     * 判断宠物是否在收藏列表中
     */
    public boolean isPetCollected(String username, Long petid) {
        List<Collect> collects = collectRepository.findByUsernameAndType(username, collectType.pet);
        return collects.stream().anyMatch(collect -> petid.equals(collect.getPetid()));
    }

    /**
     * 添加收藏
     */
    public Collect addCollect(Collect collect) {
        return collectRepository.save(collect);
    }

    public void addPetCollect(String username, Long petid, String status) {
        Collect collect = new Collect();
        collect.setUsername(username);
        collect.setType(collectType.pet);
        collect.setPetid(petid);
        collect.setStatus(status);
        collectRepository.save(collect);
    }

    public void addGoodCollect(String username, Long goodid, String status) {
        Collect collect = new Collect();
        collect.setUsername(username);
        collect.setType(collectType.good);
        collect.setGoodid(goodid);
        collect.setStatus(status);
        collectRepository.save(collect);
    }
}
